package com.csy.blog.service.service.impl;

import com.csy.blog.service.domain.User;
import com.csy.blog.service.mapper.UserMapper;
import com.csy.blog.service.service.UserService;
import com.csy.blog.service.utils.PageQuery;
import com.csy.blog.service.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.orderbyhelper.OrderByHelper;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public Result query(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPage(),pageQuery.getSize());
        Map map = pageQuery.convertFilterToMap();
        pageQuery.convertSort();
        OrderByHelper.orderBy(pageQuery.convertSort());
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getSize());
        List<User>list = userMapper.query(map);
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        return Result.success(pageInfo);
    }

    @Override
    public Result save(User user) {
        userMapper.insertSelective(user);
        return Result.success();
    }
}
